package com.rabbitmq.exec.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.exec.utils.RabbitMqUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

/**
 * @Description
 * @Author lhj
 * @Date 2024/4/14 22:33
 */
public class ReceiveLogsTopic02 {

    static final Logger logger = LoggerFactory.getLogger(ReceiveLogsTopic02.class);
    public static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        
        String queueName = "Q2";
        channel.queueDeclare(queueName, false, false, false, null);
        
        channel.queueBind(queueName, EXCHANGE_NAME, "*.*.rabbit");
        channel.queueBind(queueName, EXCHANGE_NAME, "lazy.#");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody(), StandardCharsets.UTF_8);
            logger.info("消费者02接收消息 exchange: {}, queueName: {}, msg: {}", EXCHANGE_NAME, queueName, msg);
        };
        
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
    }
    
}
